<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" placeholder="用户名手机" @focus="cleanValid('username')"></el-input>
      </el-form-item>
      <el-input
        class="annie"
        prop="captcha"
        placeholder="验证码"
        v-model="ruleForm.captcha"
        @focus="cleanValid('captcha')"
      >
        <template slot="append">
          <span @click="sendCode">验证码</span>
        </template>
      </el-input>
      <el-form-item prop="nickname">
        <el-input v-model="ruleForm.nickname" placeholder="昵称" @focus="cleanValid('nickname')"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          @focus="cleanValid('password')"
          autocomplete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="topassword">
        <el-input
          type="password"
          v-model="ruleForm.topassword"
          @focus="cleanValid('topassword')"
          autocomplete="off"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-button type="primary" class="login" @click="register">注册</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {};
    var validatePass = (rule, value, callback) => {
      // if (value === "") {
      //   callback(new Error("请输入用户名"));
      // } else {
      //   if (this.ruleForm.checkPass !== "") {
      //     this.$refs.ruleForm.validateField("checkPass");
      //   }
      //   callback();
      // }
      if (value !== this.ruleForm.password) {
        callback(new Error("请输入确认密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      // if (value === "") {
      //   callback(new Error("请输入密码"));
      // } else if (value !== this.ruleForm.pass) {
      //   callback(new Error("输入错误"));
      // } else {
      //   callback();
      // }
    };
    return {
      ruleForm: {
        username: "", //手机号码
        nickname: "", //昵称
        password: "", //密码
        captcha: "", //手机验证码
        topassword: "", //新密码
      },
      rules: {
        username: [
          // required:必填 message：如果不满足规则时的提示信息  trigger:触发校验的时机
          { required: true, message: "请输入用户名", trigger: "blur" },
          // 补充：添加用户输入内容的验证，添加正则表达式
          {
            pattern: /^1\d{10}$|^\w{3,}$/,
            message: "请输入正则格式的用户名或者11位数字的手机号",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        topassword: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 发送验证码
    sendCode() {
      // 验证手机号是否格式正确，只有手机号正确，才能接收到服务器发回的验证码
      if (/^1\d{10}$/.test(this.ruleForm.username)) {
        this.$axios({
          method: "post",
          url: "/captchas",
          data: { tel: this.ruleForm.username },
        }).then((res) => {
          console.log(res);
          this.$confirm("本次验证码为：" + res.data.code, "提示", {
            confirmButtonText: "确定", //确认按钮文本
            cancelButtonText: "取消", //取消按钮文本
            type: "success",
          });
        });
      } else {
        // 消息确认框
        this.$confirm("你所输入的手机号格式不正确", "提示", {
          confirmButtonText: "确定", //确认按钮文本
          cancelButtonText: "取消", //取消按钮文本
          type: "warning",
        });
      }
    },
    // 注册
    register() {
      let { topassword, ...props } = this.ruleForm;
      console.log(props);
      this.$axios({
        method: "post",
        url: "/accounts/register",
        data: props,
      }).then((res) => {
        console.log(res);
        // 提示
        this.$confirm("注册成功", "提示", {
          confirmButtonText: "确定", //确认按钮文本
          cancelButtonText: "取消", //取消按钮文本
          type: "success",
        }).then(() => {
          // 自动切换到登陆
          this.$emit("goLogin");
        });
      });
    },
    //   清除当前的验证
    cleanValid(propname) {
      //   // 清除指定的验证信息
      this.$refs.ruleForm.clearValidate(propname);
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  padding: 20px;
}
.login {
  width: 100%;
}
.lianjie {
  float: right;
  margin-bottom: 20px;
}
.annie {
  margin-bottom: 20px;
}
</style>